from ft_table import FtTable
from record import Record
import dlz_config as dc
DLZ_PROJECT_LIST = 440872
COMPACTION_PROJECT = 423203
COMPACTION_SCHEMA = 477986
COMPACTION = 422606
CHANGES_TABLE = 437416



def processUploadedCompaction(records):
    '''process uploaded csv file
    '''
    header = records[0]
    data = records[2:]
    ft = FtTable(None, None, COMPACTION)
    for r_uploaded in data:
        r_uploaded = dict(zip(header, r_uploaded))
        rowid = r_uploaded['rowid'].split('_')[0]
        r = Record(ft, rowid, CHANGES_TABLE)
        for fld in header:
            if fld=='rowid':
                pass
            else:
                r[fld] = r_uploaded[fld]
        r.update()
    



def parseProject_name(t):
    if len(t)==0:
        return ('', '')
    
    for idx, c in enumerate(t):
        if c in '0123456789':
            pass
        else:
            break
    
            
    return (t[:idx], t[idx:])
def get_project(project_number):
    proj_table = FtTable(None, None, int(dc.get('DLZ_PROJECT_LIST')['ft_table_id']))
    sql = "select client_name, project_name, project_number from %s where project_number = '%s'" % (proj_table.tableid, project_number)
    results = proj_table.SqlQuery(sql)
    if len(results)==0:
        return None
    return results[0]


#def update_compaction_project_table():
#    compaction_project_table = FtTable(None, None,COMPACTION_PROJECT)
#    sql = 'select project_name, rowid from %s' % compaction_project_table.tableid
#    records = compaction_project_table.SqlQuery(sql)
#    for record in records:
#        project_number = parseProject_name(record['project_name'])[0]
#        project = get_project(project_number)
#        if project:
#            data = project
#            data['tableid'] =compaction_project_table.tableid
#            data['rowid'] = record['rowid'] 
#            sql = "update %(tableid)s set client_name = '%(client_name)s', project_number = '%(project_number)s', project_name_full = '%(project_name)s' where rowid='%(rowid)s'" % data 
#            compaction_project_table.SqlQuery(sql)
#            
#            
            